<?php

namespace app\admin\controller;
 
use think\Controller;
use think\Db;
use think\Request;
use app\admin\controller\Admin;


class Cost extends Admin
{
    /**
     * 查询并渲染 成本计划总览页面
     */
    public function index()
    {
       //查询项目信息
       $data = db('project_application')
            ->field('project_name')
            ->distinct(true)
            ->select();
       // var_dump($data);die;
       //渲染页面
       return view('admin@cost/index',[
            'data'=>$data
       ]);
    }

    /**
     * 渲染成本填报页面
     * @return [type] [description]
     */
    public function costAdd()
    {   
        //查询已审核通过项目数据
        $data = db('project_application')
            ->field('project_name')
            ->distinct(true)
            ->select();
        //渲染成本填报页面
        return view('admin@cost/costAdd',[
            'data' => $data
       ]);
    }

    /**
     * 成本执行添加
     * @return [type] [description]
     */
    public function costDoAdd()
    {   
        //接收数据
        $data = input('param.');
        //判断项目是否存在,存在则返回
        if(db('cost')->where(['project_name' => $data['project_name']])->find()){
            return $this ->error('本项目成本已填报,请重新添加!');
        }
        //遍历数据,计算总价 降低额 降低率
        $num = 0;
        foreach ($data as $k => $v) {
            if($k == "project_name"){
                continue;
            }else{
                $num += $v;
            }
        }
        $data['total_cost'] = $num;
        $total_cost_plan = db('cost_plan')->field('total_cost_plan')->where(['project_name' => $data['project_name']])->find();
        $data['derate'] = floatval($total_cost_plan['total_cost_plan']) - $data['total_cost'];
        if($data['derate'] >= 0){
            $data['rate_cutting'] = round($data['derate']/$total_cost_plan['total_cost_plan'],2);
        }else{
            $data['rate_cutting'] = 0;
        }
        //插入数据
        $res = db('cost')->insert($data);
        if ($res > 0) {
            return $this->success('保存成功!', url('admin/cost/index'));
        } else {
            return $this->error('保存失败!');
        }
    }

    /**
     * 渲染成本更新页面
     * @return [type] [description]
     */
    public function costEdit()
    {
        //接收ID
        $id = input('id');
        //查询信息
        $data = db('cost')
            ->where(['id' => $id])
            ->find();
            // var_dump($data);die;
        //渲染成本更新页面
        return view('admin@cost/costEdit',[
            'data' => $data
       ]);
    } 
    /**
     * 执行成本编辑更新
     * @return [type] [description]
     */
    public function costDoEdit()
    {
        //接收数据 
        $data = input('param.');
        //遍历数据,计算总价
        $num = 0;
        foreach ($data as $k => $v) {
            if($k == "project_name"){
                continue;
            }else{
                $num += $v;
            }
        }
        $data['total_cost'] = $num;
        $total_cost_plan = db('cost_plan')->field('total_cost_plan')->where(['project_name' => $data['project_name']])->find();
        $data['derate'] = floatval($total_cost_plan['total_cost_plan']) - $data['total_cost'];
        if($data['derate'] >= 0){
            $data['rate_cutting'] = round($data['derate']/$total_cost_plan['total_cost_plan'],2);
        }else{
            $data['rate_cutting'] = 0;
        }
        //插入数据
        // var_dump($data);die;
        $res = db('cost')->where(['id' => $data['id']])->update($data);
        if ($res > 0) {
            return $this->success('更新成功!', url('admin/cost/index'));
        } else {
            return $this->error('更新失败!');
        }
        // var_dump($data);exit;
    }
    /**
     * 渲染成本计划填报页面
     * @return [type] [description]
     */
    public function costPlanAdd()
    {   
        //查询已审核项目信息
        $data = db('project_application')
            ->field('project_name')
            ->distinct(true)
            ->select();
        //渲染成本计划添加页面
        return view('admin@cost/costPlanAdd',[
            'data' => $data
       ]);
    }

    public function costPlanDoAdd()
    {
        //接收数据 
        $data = input('param.');
        //判断项目是否已填报,true则返回
        if(db('cost_plan')->where(['project_name' => $data['project_name']])->find()){
            return $this ->error('本项目计划已存在,请重新添加!');
        }
        // var_dump($data);die;
        //遍历数据,计算总价
        $num = 0;
        foreach ($data as $k => $v) {
            if($k == "project_name"){
                continue;
            }else{
                $num += $v;
            }
        }
        $data['total_cost_plan'] = $num;
        //插入数据
        $res = db('cost_plan')->insert($data);
        if ($res > 0) {
            return $this->success('保存成功!', url('admin/cost/index'));
        } else {
            return $this->error('保存失败!');
        }
        // var_dump($data);exit;
    }

    /**
     * 渲染成本计划更新页面
     * @return [type] [description]
     */
    public function costPlanEdit()
    {
        //接收ID
        $id = input('id');
        //查询信息
        $data = db('cost_plan')
            ->where(['id' => $id])
            ->find();
        //渲染成本计划更新页面
        return view('admin@cost/costPlanEdit',[
            'data' => $data
       ]);
    }
    /**
     * 执行成本计划编辑更新
     * @return [type] [description]
     */
    public function costPlanDoEdit()
    {
        //接收数据 
        $data = input('param.');
        //遍历数据,计算总价
        $num = 0;
        foreach ($data as $k => $v) {
            if($k == "project_name"){
                continue;
            }else{
                $num += $v;
            }
        }
        $data['total_cost_plan'] = $num;
        //插入数据
        // var_dump($data);die;
        $res = db('cost_plan')->where(['id' => $data['id']])->update($data);
        if ($res > 0) {
            return $this->success('更新成功!', url('admin/cost/index'));
        } else {
            return $this->error('更新失败!');
        }
        // var_dump($data);exit;
    }



    /**
     * [details 查询详情并渲染详情页面]
     * @return [type] [description]
     */
    public function details()
    {
        // return view('admin@cost/details');
        //接收数据
        $name = input('param.');

        // 查询数据库信息
        $dataPlan = db('cost_plan')->where(['project_name' => $name['project_name']])->select();
        $data = db('cost')->where(['project_name' => $name['project_name']])->select();

        $dataPlanList = [];
        foreach ($dataPlan as $key => $v) {
            foreach ($v as $r => $h) {
                if( ($r == 'id') || ($r == 'project_name') || ($r == 'status')){
                    continue;
                }else{
                    $dataPlanList[] = $h;
                }
            }
        }
        $dataPlanList = json($dataPlanList);
        // 渲染页面
        return view('admin@cost/details',[
            'data'=>$data,
            'dataPlan'=>$dataPlan,
            'dataPlanList' => json($dataPlanList)
        ]);
    }


    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function deletePlan($id)
    {   
        $res = db('cost_plan')->delete($id);
        if ($res > 0) {
            $info['status'] = true;
            $info['id'] = $id;
            $info['info'] = 'ID为'.$id.'的项目删除成功!';
        }else{
            $info['status'] = false;
            $info['id'] = $id;
            $info['info'] = '删除失败';
        }
        return json($info);
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {   
        $res = db('cost')->delete($id);
        if ($res > 0) {
            $info['status'] = true;
            $info['id'] = $id;
            $info['info'] = 'ID为'.$id.'的项目删除成功!';
        }else{
            $info['status'] = false;
            $info['id'] = $id;
            $info['info'] = '删除失败';
        }
        return json($info);
    }
}

